// $codepro.audit.disable
/**
 * NursesUI.java
 * @author Suren Nihalani
 * 
 */
// $codepro.audit.disable numericLiterals
package edu.gatech.coc.betameisters.userinterfaces;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.awt.Panel;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.ListSelectionModel;

import edu.gatech.coc.betameisters.main.MainPanel;
import edu.gatech.coc.betameisters.people.Doctor;
import edu.gatech.coc.betameisters.people.Nurse;
import edu.gatech.coc.betameisters.people.Patient;
import edu.gatech.coc.betameisters.utilities.Database;
import edu.gatech.coc.betameisters.infoholders.Appointment;
import edu.gatech.coc.betameisters.infoholders.Constants;
import edu.gatech.coc.betameisters.infoholders.DoctorsOrders;
import edu.gatech.coc.betameisters.infoholders.Invoice;
import edu.gatech.coc.betameisters.infoholders.Prescription;
import edu.gatech.coc.betameisters.infoholders.Treatment;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextPane;

/**
 * The Class NursesUI.
 * 
 * @author Suren Nihalani
 * @version $REVISION$
 */
@SuppressWarnings("serial")
public class NursesUI extends JPanel {
	/**
	 * The nurse interacting with the UI.
	 */
	private Nurse myNurse;

	/**
	 * Helps determine whether the search is by username or real name
	 */
	private boolean isSearchingByUserName;

	/**
	 * Daily appointments table including for doctors.
	 */
	private final JTable appointmentsTable;

	/**
	 * All patients table. This table will be used for instant search.
	 */
	private final JTable allPatientsTable;

	/** TextBox where nurse will type to search for the patient. */
	private final JTextField patientsSearchBox;

	/**
	 * The chosen Patients appointments table.
	 */
	private final JTable currentPatientsAppointmentsTable;

	/**
	 * The chosen patients treatments table.
	 */
	private final JTable currentPatientsTreatmentsTable;

	private final ButtonGroup buttonGroup = new ButtonGroup();

	private JTextField txtFirstName;

	private JTextField txtLastName;

	@SuppressWarnings("unused")
	private List<Appointment> appList;

	/**
	 * Field the row selected
	 */
	private int selected_row;

	@SuppressWarnings("unused")
	private MainPanel parent;
	private JPanel doctorsOrdersPanel;

	/**
	 * Create the panel.
	 * 
	 * @param theNurse
	 *            The nurse object who will be controlling the UI.
	 * @param p
	 *            Parent
	 */
	public NursesUI(Nurse theNurse,final MainPanel p) { // $codepro.audit.disable
													// com.instantiations.assist.eclipse.analysis.audit.rule.effectivejava.constructorsOnlyInvokeFinalMethods
		parent = p;
		isSearchingByUserName = false;
		myNurse = theNurse;
		selected_row = -1;
		final SpringLayout springLayout = new SpringLayout();
		setLayout(springLayout);
		final JLabel welcomeLabel = new JLabel("Welcome, " + myNurse.toString());
		springLayout.putConstraint(SpringLayout.NORTH, welcomeLabel, 10,
				SpringLayout.NORTH, this);
		springLayout.putConstraint(SpringLayout.WEST, welcomeLabel, 10,
				SpringLayout.WEST, this);
		add(welcomeLabel);

		final JButton logoutButton = new JButton("Logout");
		springLayout.putConstraint(SpringLayout.NORTH, logoutButton, 0,
				SpringLayout.NORTH, welcomeLabel);
		springLayout.putConstraint(SpringLayout.EAST, logoutButton, -10,
				SpringLayout.EAST, this);
		add(logoutButton);

		final JTabbedPane tabFolder = new JTabbedPane(SwingConstants.TOP);
		springLayout.putConstraint(SpringLayout.NORTH, tabFolder, 16,
				SpringLayout.SOUTH, welcomeLabel);
		springLayout.putConstraint(SpringLayout.WEST, tabFolder, 0,
				SpringLayout.WEST, welcomeLabel);
		springLayout.putConstraint(SpringLayout.SOUTH, tabFolder, 0,
				SpringLayout.SOUTH, this);
		springLayout.putConstraint(SpringLayout.EAST, tabFolder, 10,
				SpringLayout.EAST, logoutButton);
		add(tabFolder);

		final JPanel appointmentsPanel = new JPanel();
		tabFolder.addTab("Appointments", null, appointmentsPanel, null);
		final SpringLayout sl_AppointmentsPanel = new SpringLayout();
		appointmentsPanel.setLayout(sl_AppointmentsPanel);

		appointmentsTable = new JTable();
		sl_AppointmentsPanel.putConstraint(SpringLayout.NORTH,
				appointmentsTable, 25, SpringLayout.NORTH, appointmentsPanel);
		sl_AppointmentsPanel.putConstraint(SpringLayout.WEST,
				appointmentsTable, 10, SpringLayout.WEST, appointmentsPanel);
		sl_AppointmentsPanel.putConstraint(SpringLayout.SOUTH,
				appointmentsTable, -10, SpringLayout.SOUTH, appointmentsPanel);
		appointmentsTable.setModel(new DefaultTableModel(new Object[50][4],
				new String[] { "Doctor", "Start Time", "Patient's Real Name",
						"Patient's User Name" }));
		
		appointmentsTable.getColumnModel().getColumn(2).setPreferredWidth(76);
		appointmentsPanel.add(appointmentsTable);

		final JLabel labelPatient = new JLabel("Patient");
		sl_AppointmentsPanel.putConstraint(SpringLayout.WEST, labelPatient,
				186, SpringLayout.WEST, appointmentsPanel);
		sl_AppointmentsPanel.putConstraint(SpringLayout.SOUTH, labelPatient,
				-6, SpringLayout.NORTH, appointmentsTable);
		appointmentsPanel.add(labelPatient);

		final JLabel doctorLabel = new JLabel("Doctor");
		sl_AppointmentsPanel.putConstraint(SpringLayout.NORTH, doctorLabel, 5,
				SpringLayout.NORTH, appointmentsPanel);
		sl_AppointmentsPanel.putConstraint(SpringLayout.WEST, doctorLabel, 10,
				SpringLayout.WEST, appointmentsPanel);
		appointmentsPanel.add(doctorLabel);

		final JLabel lblStartTime = new JLabel("Start Time");
		sl_AppointmentsPanel.putConstraint(SpringLayout.NORTH, lblStartTime, 0,
				SpringLayout.NORTH, labelPatient);
		sl_AppointmentsPanel.putConstraint(SpringLayout.EAST, lblStartTime,
				-40, SpringLayout.WEST, labelPatient);
		appointmentsPanel.add(lblStartTime);

		final JButton checkInButton = new JButton("Check In");
		sl_AppointmentsPanel.putConstraint(SpringLayout.WEST, checkInButton,
				336, SpringLayout.WEST, appointmentsPanel);
		sl_AppointmentsPanel.putConstraint(SpringLayout.EAST, checkInButton,
				-10, SpringLayout.EAST, appointmentsPanel);
		sl_AppointmentsPanel.putConstraint(SpringLayout.EAST,
				appointmentsTable, -6, SpringLayout.WEST, checkInButton);
		checkInButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				myNurse.searchPatient(
						(String) appointmentsTable.getValueAt(selected_row, 3),
						true);
				myNurse.getCurrentPatient().loggedIn();
			}
		});
		appointmentsPanel.add(checkInButton);

		JButton btnRefresh = new JButton("Get/Refresh");
		sl_AppointmentsPanel.putConstraint(SpringLayout.NORTH, checkInButton,
				8, SpringLayout.SOUTH, btnRefresh);
		sl_AppointmentsPanel.putConstraint(SpringLayout.WEST, btnRefresh, 6,
				SpringLayout.EAST, appointmentsTable);
		sl_AppointmentsPanel.putConstraint(SpringLayout.NORTH, btnRefresh, -4,
				SpringLayout.NORTH, appointmentsTable);
		sl_AppointmentsPanel.putConstraint(SpringLayout.EAST, btnRefresh, -10,
				SpringLayout.EAST, appointmentsPanel);
		btnRefresh.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				int i = 0;
				Appointment temp;
				List<Appointment> appointList = Database.loadAppointment(p.getUser().getUserID());
				while (!appointList.isEmpty()){
					// search for a certain day and print all the information
					temp = appointList.remove(0);
					appointmentsTable.setValueAt(temp.getContact().getLastName(), i, 0);
					appointmentsTable.setValueAt(temp.getMyStartTime().get(Calendar.HOUR) + ":" + temp.getMyStartTime().get(Calendar.MINUTE), i, 1);
					appointmentsTable.setValueAt(temp.getPatient().getUserID(), i, 2);
					appointmentsTable.setValueAt(temp.getPatient().getUserID(), i, 3);
					i++;
					}
				}
			});
		appointmentsPanel.add(btnRefresh);

		JLabel lblPatientsUsername = new JLabel("Patient's Username");
		sl_AppointmentsPanel.putConstraint(SpringLayout.NORTH,
				lblPatientsUsername, 0, SpringLayout.NORTH, labelPatient);
		sl_AppointmentsPanel.putConstraint(SpringLayout.WEST,
				lblPatientsUsername, 33, SpringLayout.EAST, labelPatient);
		appointmentsPanel.add(lblPatientsUsername);
		
				final JPanel patientPanel = new JPanel();
				tabFolder.addTab("Patient", null, patientPanel, null);
				final SpringLayout sl_PatientPanel = new SpringLayout();
				patientPanel.setLayout(sl_PatientPanel);
				
						final JButton deleteButton = new JButton("Delete");
						deleteButton.setEnabled(false);
						
								final JButton editButton = new JButton("Edit");
								sl_PatientPanel.putConstraint(SpringLayout.NORTH, deleteButton, 6, SpringLayout.SOUTH, editButton);
								editButton.setEnabled(false);
								
										allPatientsTable = new JTable();
										sl_PatientPanel.putConstraint(SpringLayout.WEST, deleteButton, 17, SpringLayout.EAST, allPatientsTable);
										sl_PatientPanel.putConstraint(SpringLayout.WEST, editButton, 17, SpringLayout.EAST, allPatientsTable);
										sl_PatientPanel.putConstraint(SpringLayout.NORTH, allPatientsTable, 26,
												SpringLayout.NORTH, patientPanel);
										sl_PatientPanel.putConstraint(SpringLayout.WEST, allPatientsTable, 10,
												SpringLayout.WEST, patientPanel);
										sl_PatientPanel.putConstraint(SpringLayout.EAST, allPatientsTable,
												-211, SpringLayout.EAST, patientPanel);
										allPatientsTable
												.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
										sl_PatientPanel.putConstraint(SpringLayout.SOUTH, allPatientsTable,
												-10, SpringLayout.SOUTH, patientPanel);
										// test
										allPatientsTable.setModel(new DefaultTableModel(new Object[20][3],
												new String[] { "Real Name", "Username", "Information" }));
										// test end
										patientPanel.add(allPatientsTable);
										
												patientsSearchBox = new JTextField();
												sl_PatientPanel.putConstraint(SpringLayout.EAST, deleteButton, 0, SpringLayout.EAST, patientsSearchBox);
												sl_PatientPanel.putConstraint(SpringLayout.EAST, editButton, 0, SpringLayout.EAST, patientsSearchBox);
												sl_PatientPanel.putConstraint(SpringLayout.WEST, patientsSearchBox, 17, SpringLayout.EAST, allPatientsTable);
												sl_PatientPanel.putConstraint(SpringLayout.EAST, patientsSearchBox, -21, SpringLayout.EAST, patientPanel);
												patientPanel.add(patientsSearchBox);
												patientsSearchBox.setColumns(10);
												
														final JButton searchButton = new JButton("Search");
														sl_PatientPanel.putConstraint(SpringLayout.NORTH, editButton, 6, SpringLayout.SOUTH, searchButton);
														sl_PatientPanel.putConstraint(SpringLayout.WEST, searchButton, 0, SpringLayout.WEST, patientsSearchBox);
														sl_PatientPanel.putConstraint(SpringLayout.EAST, searchButton, -21, SpringLayout.EAST, patientPanel);
														searchButton.setEnabled(false);
														
																patientPanel.add(searchButton);
																patientPanel.add(editButton);
																patientPanel.add(deleteButton);
																
																		final JButton createPatientButton = new JButton("Create Patient");
																		sl_PatientPanel.putConstraint(SpringLayout.NORTH, patientsSearchBox, 39, SpringLayout.SOUTH, createPatientButton);
																		sl_PatientPanel.putConstraint(SpringLayout.WEST, createPatientButton, 17, SpringLayout.EAST, allPatientsTable);
																		sl_PatientPanel.putConstraint(SpringLayout.EAST, createPatientButton, -21, SpringLayout.EAST, patientPanel);
																		
																				patientPanel.add(createPatientButton);
																				
																						JRadioButton rdbtnRealName = new JRadioButton("Real Name");
																						sl_PatientPanel.putConstraint(SpringLayout.NORTH, searchButton, 6, SpringLayout.SOUTH, rdbtnRealName);
																						sl_PatientPanel.putConstraint(SpringLayout.NORTH, rdbtnRealName, 6, SpringLayout.SOUTH, patientsSearchBox);
																						sl_PatientPanel.putConstraint(SpringLayout.WEST, rdbtnRealName, 6, SpringLayout.EAST, allPatientsTable);
																						
																								buttonGroup.add(rdbtnRealName);
																								patientPanel.add(rdbtnRealName);
																								
																										JRadioButton rdbtnUserName = new JRadioButton("User Name");
																										sl_PatientPanel.putConstraint(SpringLayout.NORTH, rdbtnUserName, 6, SpringLayout.SOUTH, patientsSearchBox);
																										
																												buttonGroup.add(rdbtnUserName);
																												patientPanel.add(rdbtnUserName);
																												
																														JLabel lblUserName = new JLabel("User Name");
																														sl_PatientPanel.putConstraint(SpringLayout.WEST, lblUserName, 11, SpringLayout.WEST, patientPanel);
																														sl_PatientPanel.putConstraint(SpringLayout.SOUTH, lblUserName, -6, SpringLayout.NORTH, allPatientsTable);
																														patientPanel.add(lblUserName);
																														
																																JLabel lblPatientsName = new JLabel("Patient's Name");
																																sl_PatientPanel.putConstraint(SpringLayout.NORTH, lblPatientsName, 0, SpringLayout.NORTH, lblUserName);
																																patientPanel.add(lblPatientsName);
																																
																																		JLabel lblSomething = new JLabel("Something");
																																		sl_PatientPanel.putConstraint(SpringLayout.EAST, lblPatientsName, -70, SpringLayout.WEST, lblSomething);
																																		sl_PatientPanel.putConstraint(SpringLayout.NORTH, lblSomething, 0, SpringLayout.NORTH, lblUserName);
																																		patientPanel.add(lblSomething);
																																		
																																				txtFirstName = new JTextField();
																																				sl_PatientPanel.putConstraint(SpringLayout.WEST, txtFirstName, 16, SpringLayout.EAST, allPatientsTable);
																																				sl_PatientPanel.putConstraint(SpringLayout.EAST, txtFirstName, -100, SpringLayout.EAST, createPatientButton);
																																				sl_PatientPanel.putConstraint(SpringLayout.NORTH, createPatientButton, 6, SpringLayout.SOUTH, txtFirstName);
																																				sl_PatientPanel.putConstraint(SpringLayout.NORTH, txtFirstName, -3, SpringLayout.NORTH, allPatientsTable);
																																				patientPanel.add(txtFirstName);
																																				txtFirstName.setColumns(10);
																																				
																																						txtLastName = new JTextField();
																																						sl_PatientPanel.putConstraint(SpringLayout.WEST, txtLastName, 10, SpringLayout.EAST, txtFirstName);
																																						sl_PatientPanel.putConstraint(SpringLayout.WEST, rdbtnUserName, 0, SpringLayout.WEST, txtLastName);
																																						sl_PatientPanel.putConstraint(SpringLayout.NORTH, txtLastName, -3, SpringLayout.NORTH, allPatientsTable);
																																						sl_PatientPanel.putConstraint(SpringLayout.EAST, txtLastName, -21, SpringLayout.EAST, patientPanel);
																																						patientPanel.add(txtLastName);
																																						txtLastName.setColumns(10);
																																						
																																								JLabel lblFirstName = new JLabel("First Name");
																																								sl_PatientPanel.putConstraint(SpringLayout.EAST, lblSomething, -105, SpringLayout.WEST, lblFirstName);
																																								sl_PatientPanel.putConstraint(SpringLayout.NORTH, lblFirstName, 0, SpringLayout.NORTH, lblUserName);
																																								patientPanel.add(lblFirstName);
																																								
																																										JLabel lblLastName = new JLabel("Last Name");
																																										sl_PatientPanel.putConstraint(SpringLayout.EAST, lblFirstName, -45, SpringLayout.WEST, lblLastName);
																																										sl_PatientPanel.putConstraint(SpringLayout.NORTH, lblLastName, 0, SpringLayout.NORTH, lblUserName);
																																										sl_PatientPanel.putConstraint(SpringLayout.EAST, lblLastName, -48, SpringLayout.EAST, patientPanel);
																																										patientPanel.add(lblLastName);
																																										
																																												editButton.addMouseListener(new MouseAdapter() {
																																													@Override
																																													public void mouseClicked(MouseEvent arg0) {
																																														if (selected_row != -1) {
																																															String username = (String) allPatientsTable.getValueAt(
																																																	selected_row, 0);
																																															myNurse.searchPatient(username, true);
																																															parent.setMedicalHistory(myNurse.getCurrentPatient(), myNurse.getCurrentPatient().getMedHist());
																																															System.out.println("Editing Patient's Details");
																																															// could display an error window here
																																														}
																																													}
																																												});
																																												
																																														deleteButton.addMouseListener(new MouseAdapter() {
																																															@Override
																																															public void mouseClicked(MouseEvent arg0) {
																																																if (selected_row != -1) {
																																																	String username = (String) allPatientsTable.getValueAt(
																																																			selected_row, 0);
																																																	if (myNurse.deletePatient(username)) {
																																																		System.out.println("Patient was deleted froms system");
																																																	} else {
																																																		System.out
																																																				.println("Patient was not deleted froms system");
																																																	}
																																																	for (int i = 0; i < allPatientsTable.getColumnCount(); i++) {
																																																		allPatientsTable.setValueAt("", selected_row, i);
																																																	}
																																																	// could display an error window here
																																																}
																																															}
																																														});
																																														
																																																searchButton.addMouseListener(new MouseAdapter() {
																																																	@Override
																																																	public void mouseClicked(MouseEvent arg0) {
																																																		if (searchButton.isEnabled()) {
																																																			List<String> aList = myNurse.searchPatient(
																																																					patientsSearchBox.getText(), isSearchingByUserName);
																																																			for (int j = 0; j < allPatientsTable.getRowCount(); j++) {
																																																				allPatientsTable.setValueAt("", j, 0);
																																																				allPatientsTable.setValueAt("", j, 1);
																																																				allPatientsTable.setValueAt("", j, 2);
																																																			}
																																																			for (int i = 0; i < aList.size(); i++) {																																		
																																																				myNurse.searchPatient(aList.get(i), true);
																																																				Patient aPatient = myNurse.getCurrentPatient();
																																																				allPatientsTable.setValueAt(aPatient.getUserID(), i, 0);
																																																				allPatientsTable.setValueAt(aPatient.getLastName()
																																																						+ " " + aPatient.getFirstName(), i, 1);
																																																				allPatientsTable.setValueAt("", i, 2);
																																																			}
																																																		}
																																																	}
																																																});
																																																
																																																		allPatientsTable.addMouseListener(new MouseAdapter() {
																																																			@Override
																																																			public void mouseClicked(MouseEvent arg0) {
																																																				deleteButton.setEnabled(true);
																																																				editButton.setEnabled(true);
																																																				selected_row = allPatientsTable.getSelectedRow();
																																																			}
																																																		});
																																																		
																																																				createPatientButton.addMouseListener(new MouseAdapter() {
																																																					@Override
																																																					public void mouseClicked(MouseEvent arg0) {
																																																						myNurse.createPatient(txtFirstName.getText(),
																																																								txtLastName.getText());
																																																					}
																																																				});
																																																				
																																																						rdbtnRealName.addMouseListener(new MouseAdapter() {
																																																							@Override
																																																							public void mouseClicked(MouseEvent e) {
																																																								isSearchingByUserName = false;
																																																								searchButton.setEnabled(true);
																																																							}
																																																						});
																																																						
																																																								rdbtnUserName.addMouseListener(new MouseAdapter() {
																																																									@Override
																																																									public void mouseClicked(MouseEvent e) {
																																																										isSearchingByUserName = true;
																																																										searchButton.setEnabled(true);
																																																									}
																																																								});

		final JPanel currentPatientPanel = new JPanel();
		tabFolder.addTab("Current Patient", null, currentPatientPanel, null);
		final SpringLayout sl_currentPatientPanel = new SpringLayout();
		currentPatientPanel.setLayout(sl_currentPatientPanel);

		final JTabbedPane currentPatientTabFolder = new JTabbedPane(
				SwingConstants.TOP);
		sl_currentPatientPanel.putConstraint(SpringLayout.NORTH,
				currentPatientTabFolder, 10, SpringLayout.NORTH,
				currentPatientPanel);
		sl_currentPatientPanel.putConstraint(SpringLayout.WEST,
				currentPatientTabFolder, 10, SpringLayout.WEST,
				currentPatientPanel);
		sl_currentPatientPanel.putConstraint(SpringLayout.SOUTH,
				currentPatientTabFolder, 432, SpringLayout.NORTH,
				currentPatientPanel);
		sl_currentPatientPanel.putConstraint(SpringLayout.EAST,
				currentPatientTabFolder, 531, SpringLayout.WEST,
				currentPatientPanel);
		currentPatientPanel.add(currentPatientTabFolder);

		final Panel currentPatientsTreatments = new Panel();
		currentPatientTabFolder.addTab("Treatments", null,
				currentPatientsTreatments, null);
		final SpringLayout sl_currentPatientsTreatments = new SpringLayout();
		currentPatientsTreatments.setLayout(sl_currentPatientsTreatments);

		currentPatientsTreatmentsTable = new JTable();
		sl_currentPatientsTreatments.putConstraint(SpringLayout.NORTH,
				currentPatientsTreatmentsTable, 32, SpringLayout.NORTH,
				currentPatientsTreatments);
		sl_currentPatientsTreatments.putConstraint(SpringLayout.WEST,
				currentPatientsTreatmentsTable, 10, SpringLayout.WEST,
				currentPatientsTreatments);
		sl_currentPatientsTreatments.putConstraint(SpringLayout.SOUTH,
				currentPatientsTreatmentsTable, -10, SpringLayout.SOUTH,
				currentPatientsTreatments);
		sl_currentPatientsTreatments.putConstraint(SpringLayout.EAST,
				currentPatientsTreatmentsTable, -204, SpringLayout.EAST,
				currentPatientsTreatments);
		currentPatientsTreatments.add(currentPatientsTreatmentsTable);
		currentPatientsTreatmentsTable.setModel(new DefaultTableModel(new Object[20][2],
				new String[] {"Treatment Type", "Treatment Description"}));

		JButton btnCreateNewTreatment = new JButton("Create New Treatment");
		sl_currentPatientsTreatments.putConstraint(SpringLayout.NORTH, btnCreateNewTreatment, 10, SpringLayout.NORTH, currentPatientsTreatments);
		sl_currentPatientsTreatments.putConstraint(SpringLayout.WEST, btnCreateNewTreatment, 12, SpringLayout.EAST, currentPatientsTreatmentsTable);
		btnCreateNewTreatment.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				parent.settEditor(new Treatment(Constants.VisitType.OFFICEVISIT));
			}
		});
		currentPatientsTreatments.add(btnCreateNewTreatment);

		JButton btnModifyTreatment = new JButton("Modify Treatment");
		sl_currentPatientsTreatments.putConstraint(SpringLayout.NORTH, btnModifyTreatment, 12, SpringLayout.SOUTH, btnCreateNewTreatment);
		sl_currentPatientsTreatments.putConstraint(SpringLayout.WEST, btnModifyTreatment, 26, SpringLayout.EAST, currentPatientsTreatmentsTable);
		btnModifyTreatment.setEnabled(false);
		currentPatientsTreatments.add(btnModifyTreatment);

		final JPanel currentPatientsAppointments = new JPanel();
		currentPatientTabFolder.addTab("Appointments", null,
				currentPatientsAppointments, null);
		final SpringLayout sl_currentPatientsAppointments = new SpringLayout();
		currentPatientsAppointments.setLayout(sl_currentPatientsAppointments);

		currentPatientsAppointmentsTable = new JTable();
		sl_currentPatientsAppointments.putConstraint(SpringLayout.NORTH,
				currentPatientsAppointmentsTable, 32, SpringLayout.NORTH,
				currentPatientsAppointments);
		sl_currentPatientsAppointments.putConstraint(SpringLayout.WEST, currentPatientsAppointmentsTable, 10, SpringLayout.WEST, currentPatientsAppointments);
		sl_currentPatientsAppointments.putConstraint(SpringLayout.SOUTH,
				currentPatientsAppointmentsTable, -10, SpringLayout.SOUTH,
				currentPatientsAppointments);
		sl_currentPatientsAppointments.putConstraint(SpringLayout.EAST, currentPatientsAppointmentsTable, -143, SpringLayout.EAST, currentPatientsAppointments);
		currentPatientsAppointments.add(currentPatientsAppointmentsTable);
		currentPatientsAppointmentsTable.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
			},
			new String[] {
				"Doctor", "Start Time", "Patient's Real Name", "Patient's User Name"
			}
		));
		
		int i = 0;
		Appointment temp;
		List<Appointment> appointList = Database.loadAppointment(myNurse.getUserID());
		System.out.println(myNurse.getUserID());
		while (!appointList.isEmpty()){
			// search for a certain day and print all the information
			temp = appointList.remove(0);
			currentPatientsAppointmentsTable.setValueAt(temp.getContact().getLastName(), i, 0);
			currentPatientsAppointmentsTable.setValueAt(temp.getMyStartTime().get(Calendar.HOUR) + ":" + temp.getMyStartTime().get(Calendar.MINUTE), i, 1);
			currentPatientsAppointmentsTable.setValueAt(temp.getPatient().getUserID(), i, 2);
			currentPatientsAppointmentsTable.setValueAt(temp.getPatient().getUserID(), i, 3);
			i++;
			}

		final JButton btnNewAppointment = new JButton("New Appointment");
		sl_currentPatientsAppointments.putConstraint(SpringLayout.NORTH, btnNewAppointment, 32, SpringLayout.NORTH, currentPatientsAppointments);
		sl_currentPatientsAppointments.putConstraint(SpringLayout.WEST, btnNewAppointment, 6, SpringLayout.EAST, currentPatientsAppointmentsTable);
		sl_currentPatientsAppointments.putConstraint(SpringLayout.EAST, btnNewAppointment, -10, SpringLayout.EAST, currentPatientsAppointments);
		btnNewAppointment.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				p.setUser(myNurse);
				p.setLocal(myNurse.getCurrentPatient());
				Appointment temp = new Appointment();
				parent.setAppointment(temp);
			}
		});
		currentPatientsAppointments.add(btnNewAppointment);

		final JButton btnEdit_1 = new JButton("Edit Appointment");
		sl_currentPatientsAppointments.putConstraint(SpringLayout.WEST, btnEdit_1, 7, SpringLayout.EAST, currentPatientsAppointmentsTable);
		sl_currentPatientsAppointments.putConstraint(SpringLayout.EAST, btnEdit_1, -10, SpringLayout.EAST, currentPatientsAppointments);
		btnEdit_1.setEnabled(false);
		sl_currentPatientsAppointments.putConstraint(SpringLayout.NORTH,
				btnEdit_1, 6, SpringLayout.SOUTH, btnNewAppointment);
		currentPatientsAppointments.add(btnEdit_1);

		final JButton btnDeleteAppointment = new JButton("Delete Appointment");
		sl_currentPatientsAppointments.putConstraint(SpringLayout.WEST, btnDeleteAppointment, 6, SpringLayout.EAST, currentPatientsAppointmentsTable);
		btnDeleteAppointment.setEnabled(false);
		sl_currentPatientsAppointments.putConstraint(SpringLayout.NORTH,
				btnDeleteAppointment, 6, SpringLayout.SOUTH, btnEdit_1);
		currentPatientsAppointments.add(btnDeleteAppointment);
		
		JButton button = new JButton("Print Invoice");
		button.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				Invoice sheet = new Invoice("");
				parent.showInvoice(sheet);
			}
		});
		sl_currentPatientsAppointments.putConstraint(SpringLayout.NORTH, button, 6, SpringLayout.SOUTH, btnDeleteAppointment);
		sl_currentPatientsAppointments.putConstraint(SpringLayout.EAST, button, -23, SpringLayout.EAST, currentPatientsAppointments);
		currentPatientsAppointments.add(button);

		doctorsOrdersPanel = new JPanel();
		if (myNurse.getUserType() == Constants.UserType.DOCTOR) {
			//IN
			p.setItem(new DoctorsOrders("","",""));
			tabFolder.addTab("Doctors Order", null, doctorsOrdersPanel, null);
			//IN	
		}
		

		JButton submit = new JButton("Submit Changes");
		
		
		final JTextPane txtComments = new JTextPane();
		txtComments.setText((String) null);
		
		JLabel label = new JLabel("Doctor's Comments");
		
		final JTextPane txtMessage = new JTextPane();
		txtMessage.setText((String) null);
		
		JLabel label_1 = new JLabel("Doctor's Message");
		
		JButton viewModifyPrescrip = new JButton("View/Modify Prescription");
		viewModifyPrescrip.setEnabled(false);
		
		JButton createPrescrip = new JButton("Create New Prescription");
		createPrescrip.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				p.setpEditor(new Prescription((Doctor) p.getUser(),(Patient) p.getLocal()));
			}
		});
		
		JButton viewModifyTreat = new JButton("View/Modify Treatment");
		viewModifyTreat.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				
			}
		});
		viewModifyTreat.setEnabled(false);
		
		JButton buttonCreateTreat = new JButton("Create New Treatment");
		buttonCreateTreat.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				p.setLocal(myNurse.getCurrentPatient());
				p.settEditor(new Treatment(null));
			}
		});
		
		JButton createViewReport = new JButton("Create/View Lab Work Report");
		GroupLayout gl_doctorsOrdersPanel = new GroupLayout(doctorsOrdersPanel);
		gl_doctorsOrdersPanel.setHorizontalGroup(
			gl_doctorsOrdersPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_doctorsOrdersPanel.createSequentialGroup()
					.addGap(45)
					.addGroup(gl_doctorsOrdersPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_doctorsOrdersPanel.createSequentialGroup()
							.addGroup(gl_doctorsOrdersPanel.createParallelGroup(Alignment.LEADING)
								.addComponent(buttonCreateTreat, GroupLayout.PREFERRED_SIZE, 143, GroupLayout.PREFERRED_SIZE)
								.addComponent(createPrescrip, GroupLayout.PREFERRED_SIZE, 157, GroupLayout.PREFERRED_SIZE)
								.addComponent(viewModifyPrescrip, GroupLayout.PREFERRED_SIZE, 157, GroupLayout.PREFERRED_SIZE)
								.addGroup(gl_doctorsOrdersPanel.createSequentialGroup()
									.addComponent(label_1, GroupLayout.PREFERRED_SIZE, 93, GroupLayout.PREFERRED_SIZE)
									.addGap(205)
									.addComponent(label, GroupLayout.PREFERRED_SIZE, 93, GroupLayout.PREFERRED_SIZE))
								.addGroup(gl_doctorsOrdersPanel.createSequentialGroup()
									.addComponent(txtMessage, GroupLayout.PREFERRED_SIZE, 209, GroupLayout.PREFERRED_SIZE)
									.addGap(89)
									.addComponent(txtComments, GroupLayout.PREFERRED_SIZE, 207, GroupLayout.PREFERRED_SIZE)))
							.addContainerGap(97, Short.MAX_VALUE))
						.addGroup(Alignment.TRAILING, gl_doctorsOrdersPanel.createSequentialGroup()
							.addGroup(gl_doctorsOrdersPanel.createParallelGroup(Alignment.TRAILING)
								.addComponent(submit, GroupLayout.PREFERRED_SIZE, 115, GroupLayout.PREFERRED_SIZE)
								.addGroup(gl_doctorsOrdersPanel.createSequentialGroup()
									.addComponent(viewModifyTreat, GroupLayout.PREFERRED_SIZE, 143, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED, 196, Short.MAX_VALUE)
									.addComponent(createViewReport, GroupLayout.PREFERRED_SIZE, 185, GroupLayout.PREFERRED_SIZE)))
							.addGap(78))))
		);
		gl_doctorsOrdersPanel.setVerticalGroup(
			gl_doctorsOrdersPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_doctorsOrdersPanel.createSequentialGroup()
					.addGap(45)
					.addComponent(buttonCreateTreat)
					.addGap(9)
					.addGroup(gl_doctorsOrdersPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(viewModifyTreat)
						.addComponent(createViewReport))
					.addGap(43)
					.addComponent(createPrescrip)
					.addGap(11)
					.addComponent(viewModifyPrescrip)
					.addGap(66)
					.addGroup(gl_doctorsOrdersPanel.createParallelGroup(Alignment.LEADING)
						.addComponent(label_1)
						.addComponent(label))
					.addGap(3)
					.addGroup(gl_doctorsOrdersPanel.createParallelGroup(Alignment.LEADING, false)
						.addComponent(txtMessage)
						.addComponent(txtComments, GroupLayout.DEFAULT_SIZE, 84, Short.MAX_VALUE))
					.addPreferredGap(ComponentPlacement.RELATED, 47, Short.MAX_VALUE)
					.addComponent(submit)
					.addGap(77))
		);
		doctorsOrdersPanel.setLayout(gl_doctorsOrdersPanel);
		
		/*
		 * JLabel currentPatientLabel = new
		 * JLabel("You are currently servicing " +
		 * myNurse.getCurrentPatient().getLastName());
		 * springLayout.putConstraint(SpringLayout.NORTH, currentPatientLabel,
		 * 0, SpringLayout.NORTH, welcomeLabel);
		 * springLayout.putConstraint(SpringLayout.WEST, currentPatientLabel,
		 * 31, SpringLayout.EAST, welcomeLabel); add(currentPatientLabel);
		 */

		// Action Listeners
		appointmentsTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				selected_row = appointmentsTable.getSelectedRow();
			}
		});
		logoutButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				System.out.println("Pressed Logout");
			}
		});
		
		submit.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				Patient aPat = (Patient) parent.getLocal();
				DoctorsOrders order = p.getItem();
				order.setComments(txtComments.getText());
				order.setMessage(txtMessage.getText());
				aPat.setMyDocOrders(p.getItem(), myNurse.getUserID());
			}
		});
	}

	/**
	 * Gets the appointments table.
	 * 
	 * @return the appointments table
	 */
	public JTable getAppointmentsTable() {
		return appointmentsTable;
	}

	/**
	 * Gets the all patients table.
	 * 
	 * @return the all patients table
	 */
	public JTable getAllPatientsTable() {
		return allPatientsTable;
	}

	/**
	 * Getter method for the search box searching all the patients.
	 * 
	 * @return the textBox where nurse will type to search for the patient
	 */
	public JTextField getPatientsSearchBox() {
		return patientsSearchBox;
	}

	/**
	 * Getter method for the treatments of the selected patient
	 * 
	 * @return the chosen patients treatments table
	 */
	public JTable getCurrentPatientsTreatmentsTable() {
		return currentPatientsTreatmentsTable;
	}

	/**
	 * Getter method for the selected patients appointments table.
	 * 
	 * @return JTable reference to the table.
	 */
	public JTable getCurrentPatientsAppointmentsTable() {
		return currentPatientsAppointmentsTable;
	}

	public Nurse getMyNurse() {
		return myNurse;
	}
}
